<?php
include_once("common.php");

/* Handles dates of any range with the formats dd/mm/yyyy or dd-mm-yyyy */
class Date {
	var $text; // Date formated as d-m-Y
	var $dd;
	var $mm;
	var $yy;
	var $valid;

	// $dateStr can be a string date or a timestamp
	function Date($dateOrTimestamp = '', $format = 'd-m-Y') {
		$this->setDate($dateOrTimestamp, $format);
	}

	/* Parsea la fecha pasada por parametro y la almacena.
	Las partes individuales son almacenadas en $dd, $mm y $yy.
	La variable $test almacena el testo igual a como lo indico el usuario.
	
	param $dateOrTimestamp puede ser un string con la fecha o un numero con el timestamp
	param $format cualquier combinacion de las letras 'y', 'm' y 'd' divididas por el caracter '-' */
	function setDate($dateOrTimestamp = '', $format = 'd-m-Y') {
		if( is_numeric($dateOrTimestamp) ) {
			$dateOrTimestamp = date('d-m-Y', $dateOrTimestamp);
			$format = 'd-m-Y';
		} else if( isEmpty($dateOrTimestamp) ) {
			$dateOrTimestamp = date('d-m-Y');
			$format = 'd-m-Y';
		}
		$this->_parse($dateOrTimestamp, $format); 
	}

	function getDbDate($onlyValid = true) {
		if($onlyValid && !$this->isValid())
			return '';
		return "$this->yy-". $this->mm ."-". $this->dd;
	}
	
	function getString($onlyValid = true) {
		if($onlyValid && !$this->isValid())
			return '';
		return $this->_padNum($this->dd) ."/". $this->_padNum($this->mm) ."/$this->yy";
	}
	
	function getTimestamp($onlyValid = true) {
		if($onlyValid && !$this->isValid())
			return '';
		return mktime(0, 0, 0, $this->mm, $this->dd, $this->yy);
	}
	
	function isValid() {
		$this->_validate();
		return $this->valid;
	}
	
	function isLeap()
	{
		if(!$this->isValid())
			return false;
		if( $this->yy % 4 == 0 ) {
			if( ($this->yy % 100 != 0) || ($this->yy % 400 == 0) ) {
				return true;
			}
		}
		return false;
	}
	
	function _validate()
	{
		if(isset($this->valid))
			return;
		if ( isEmpty($this->text) ||
			!( is_numeric($this->dd) && is_numeric($this->mm) && is_numeric($this->yy) )) {
			$this->valid = false;
		} else {
			$this->valid = checkdate($this->mm, $this->dd, $this->yy);
		}
	}
	
	function _parse($date, $format) {
		unset($this->dd, $this->mm, $this->yy, $this->valid);
		$this->text = str_replace("/", "-", $date);
		$textParts = explode("-", $this->text);
		$format = explode("-", strtolower($format));
		if(count($format) == 3 && count($textParts) == 3) {
			$parts = array();
			for($i = 0; $i < 3; $i++)
				$parts[$format[$i]] = $textParts[$i];
			$this->yy = intval($parts['y']);
			$this->mm = intval($parts['m']);
			$this->dd = intval($parts['d']);
			if($this->yy >= 0 && $this->yy <= 69) {
				$this->yy = $this->yy + 2000;
			} else if($this->yy >= 70 && $this->yy <= 100) {
				$this->yy = $this->yy + 1900;
			}
		}
	}
	
	function _padNum($num) {
		if($num >= 0 && $num <= 9)
			return "0". $num;
		return $num;
	}
}

// TODO implement Time and DateTime classes
?>